import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

public class RedisDemoList {
    public static void test01(Jedis jedis) {
        System.out.println("lpush 和 lrange");
        jedis.flushAll();
        jedis.lpush("key", "111", "222", "333");
        //可以支持负数
        List<String> result = jedis.lrange("key", 0, -1);
        System.out.println("result = " + result);
    }

    public static void test02(Jedis jedis) {
        System.out.println("rpush 尾插");
        jedis.flushAll();
        jedis.rpush("key", "111", "222", "333");
        List<String> result = jedis.lrange("key", 0, -1);
        System.out.println("result = " + result);
    }

    public static void test03(Jedis jedis) {
        System.out.println("lpop");
        jedis.flushAll();
        jedis.rpush("key", "111", "222", "333");

        String result = jedis.lpop("key");
        System.out.println("result = " + result);

        result = jedis.lpop("key");
        System.out.println("result = " + result);

        result = jedis.lpop("key");
        System.out.println("result = " + result);

        result = jedis.lpop("key");
        System.out.println("result = " + result);
    }

    public static void test04(Jedis jedis){
        System.out.println("rpop");
        jedis.flushAll();
        jedis.rpush("key", "111", "222", "333");

        String result = jedis.rpop("key");
        System.out.println("result = " + result);

        result = jedis.rpop("key");
        System.out.println("result = " + result);

        result = jedis.rpop("key");
        System.out.println("result = " + result);

        result = jedis.rpop("key");
        System.out.println("result = " + result);
    }

    public static void test05(Jedis jedis){
        System.out.println("blpop");
        jedis.flushAll();
        jedis.lpush("key","111","222","333");
        List<String> results = jedis.blpop(100, "key");
        System.out.println("key：" + results.get(0));
        System.out.println("value：" + results.get(1));
    }

    public static void test06(Jedis jedis){
        System.out.println("llen");
        jedis.flushAll();
        jedis.lpush("key","111","222","333");
        long result = jedis.llen("key");
        System.out.println("result = " + result);
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        try (Jedis jedis = jedisPool.getResource()) {
//            test01(jedis);
//            test02(jedis);
//            test03(jedis);
//            test04(jedis);
//            test05(jedis);
            test06(jedis);
        }
    }
}
